package com.dc.business.datacollectbusiness.service.impl;

import com.dc.business.datacollectbusiness.entity.Collector;
import com.dc.business.datacollectbusiness.entity.CollectorPassage;
import com.dc.business.datacollectbusiness.mapper.CollectorMapper;
import com.dc.business.datacollectbusiness.mapper.CollectorPassageMapper;
import com.dc.business.datacollectbusiness.service.CollectorService;
import com.dc.business.datacollectbusiness.utils.HttpPostToRealCollectorUtil;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CollectorServiceImpl implements CollectorService {
    @Autowired
    private CollectorMapper collectorMapper;
    @Autowired
    CollectorPassageMapper collectorPassageMapper;
    @Override
    public List<Collector> getAllCollector(int page, int size) {
        List<Collector> collectorList=new ArrayList<Collector>();
        int startIndex = (page - 1) * size;

        collectorList=collectorMapper.selectAllCollector(startIndex, size);
        return  collectorList;
    }

    @Override
    public Collector getCollectorByCollectorNumber(String collectNumber) {
        return collectorMapper.selectCollectorByCollectorNumber(collectNumber);
    }

    @Override
    public Collector getCollectorByCollectorName(String collectorName) {
        return collectorMapper.selectCollectorByCollectorName(collectorName);
    }

    @Override
    public void addCollector(Collector collector) {
        Map<String,Object> map = new HashMap<String,Object>();
        map= ThreadLocalUtil.get();
        collector.setCreateBy(map.get("username").toString());
        collector.setUpdateBy(map.get("username").toString());
        collectorMapper.insertCollector(collector);
        try {
            Collector collector1=collectorMapper.selectCollectorByCollectorNumber(collector.getCollectNumber());
            for(int i=0;i<=collector1.getPassagenSum()-1;i++) {
                CollectorPassage collectorPassage = new CollectorPassage();
                collectorPassage.setCollectorId(collector1.getCollectorId());
                collectorPassage.setCollectorCode(collector.getCollectNumber());
                collectorPassage.setState("未连接");
                collectorPassage.setPassageCode(i+1);
                String strI=String.valueOf(i+1);
//                String str=collector1.getCollectNumber().substring(0,collector1.getCollectNumber().length()-1);
                collectorPassage.setCollectorPassageCode(collector.getCollectNumber()+strI);
                collectorPassageMapper.insertCollectorPassage(collectorPassage);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    @Override
    public String updateCollector(Collector collector) {
        Map<String,Object> map = new HashMap<String,Object>();
        map= ThreadLocalUtil.get();
        collector.setUpdateBy(map.get("username").toString());
        collectorMapper.updateCollector(collector);
        //下发采集器
        String response=HttpPostToRealCollectorUtil.postToRealCollector(collector);
        return response;
    }

    @Override
    public Boolean deleteCollectorByCollectorId(Integer collectorId) {
        Collector collector=collectorMapper.selectCollectorByCollectorId(collectorId);
        List<CollectorPassage> collectorPassageList=collectorPassageMapper.selectCollectorPassageByCollectorCode(collector.getCollectNumber());
        Boolean delete=false;
        for(CollectorPassage collectorPassage:collectorPassageList) {

            if(collectorPassage.getState()==null){
                delete=true;
            }else {
                if(collectorPassage.getState().equals("已连接")||collectorPassage.equals("已关联")){
                    delete=false;
                    break;
                }else {
                    delete=true;
                }
            }

        }
        if (delete){
            collectorMapper.deleteCollectorByCollectorId(collectorId);
            for(CollectorPassage collectorPassage:collectorPassageList) {
                collectorPassageMapper.deleteCollectorPassageBycollectPassageId(collectorPassage.getCollectPassageId());
            }
            return delete;
        }else {
            return delete;
        }
    }

    @Override
    public int getAllCollectorsum() {
        return collectorMapper.getAllCollectorsum();
    }



}
